package com.mew.slots.core.game.tgg.service;

import com.alibaba.fastjson.JSON;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.entity.game.WinFree;
import com.mew.slots.core.common.game.BaseGameService;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.roller.ili.InRoom;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.mew.slots.core.game.tgg.dto.TggPlayMsgs;
import com.mew.slots.core.game.tgg.message.TggResponseMessage;
import com.mew.slots.core.game.tgg.roller.TggIli;
import com.mew.slots.core.game.tgg.room.TggRoom;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author Dylin
 */
@Service
@Slf4j
public class TggService extends BaseGameService {
    @Override
    public BasePlayMsgs startGame(StrategyGameVO player) {
        InRoom in = new InRoom();
        if (StringUtils.isNotEmpty(player.getWinFree().getJson())){
            try {
                in = JSON.parseObject(player.getWinFree().getJson(), InRoom.class);
            }catch (Exception e){
                log.debug("WinFree json解析出错：{}",player.getWinFree().getJson());
            }
        }
        TggRoom room = new TggRoom(player.getWinFree().getFreeRate());
        room.init(player.getRollerId(), TggIli.getInstance(),in,player.getWinType()==SlotsWinType.Free.code(),player.getInBetNumber(),player.getControlType(),50);
        room.roundPlayIli243();
        player.getWinFree().setJson(JSON.toJSONString(in));
        return room.getRes();
    }

    @Override
    public SlotsPlayResponseMessage myResp(WinFree winFree, BasePlayMsgs res) {
        TggResponseMessage message = new TggResponseMessage();
        TggPlayMsgs tggPlayMsgs=(TggPlayMsgs) res;
        //进入免费 设置倍数
        if(res.free==1){
            winFree.setFreeType(tggPlayMsgs.getFreeType());
            winFree.setFreeRate(tggPlayMsgs.getFreeType());
        }
        message.setFreeType(winFree.getFreeType());
        message.setFreeRate(winFree.getFreeRate());
        //免费次数大于1 累加免费倍数用作下一次免费
        if(winFree.getFreeNumber()>1){
            winFree.setFreeRate(winFree.getFreeRate()+winFree.getFreeType());
        }
        //最后一把游戏 剔除免费倍数和类型
        else if(winFree.getFreeNumber()==1){
            winFree.setFreeType(0);
            winFree.setFreeRate(0);
        }

        return message;
    }
    @Override
    public void myAfterResp(SlotsPlayResponseMessage message,StrategyGameVO player, BasePlayMsgs res) {}
}
